package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.login.TsLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album/trackInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class TrackInfoApiController {

    @Resource
    private TrackInfoService trackInfoService;

    @Resource
    private VodService vodService;

    @TsLogin
    @Operation(summary = "上传声音")
    @PostMapping("/uploadTrack")
    public Result<Map<String, Object>> uploadTrack(MultipartFile file) {
        Map<String, Object> map = vodService.uploadTrack(file);
        return Result.ok(map);
    }

    @TsLogin
    @Operation(summary = "新增声音")
    @PostMapping("/saveTrackInfo")
    public Result saveTrackInfo(@RequestBody @Validated TrackInfoVo trackInfoVo) {
        Long userId = AuthContextHolder.getUserId();
        //	调用服务层方法
        trackInfoService.saveTrackInfo(trackInfoVo, userId);
        return Result.ok();
    }

    @TsLogin
    @Operation(summary = "分页查询用户自己的声音列表")
    @PostMapping("/findUserTrackPage/{page}/{limit}")
    public Result<IPage<TrackListVo>> findUserTrackPage(@PathVariable Integer page
            , @PathVariable Integer limit
            , @RequestBody TrackInfoQuery trackInfoQuery) {
        Long userId = AuthContextHolder.getUserId();
        trackInfoQuery.setUserId(userId);
        Page<TrackListVo> trackListVoPage = new Page(page, limit);
        IPage<TrackListVo> trackListVoIPage = trackInfoService.findUserTrackPage(trackListVoPage, trackInfoQuery);
        return Result.ok(trackListVoIPage);
    }

    @TsLogin
    @Operation(summary = "根据id删除声音")
    @DeleteMapping("/removeTrackInfo/{trackId}")
    public Result removeTrackInfo(@PathVariable Integer trackId) {
        trackInfoService.removeTrackInfo(trackId);
        return Result.ok();
    }

    @Operation(summary = "根据id查询数据")
    @GetMapping("/getTrackInfo/{trackId}")
    public Result<TrackInfo> getTrackInfo(@PathVariable Integer trackId) {
        TrackInfo trackInfo = trackInfoService.getTrackInfo(trackId);
        return Result.ok(trackInfo);
    }

    @TsLogin
    @Operation(summary = "修改声音")
    @PutMapping("/updateTrackInfo/{trackId}")
    public Result updateTrackInfo(@PathVariable Integer trackId, @RequestBody @Validated TrackInfoVo trackInfoVo) {
        trackInfoService.updateTrackInfo(trackId, trackInfoVo);
        return Result.ok();
    }

    @TsLogin(required = false)
    @Operation(summary = "专辑对应的声音列表")
    @GetMapping("/findAlbumTrackPage/{albumId}/{page}/{limit}")
    public Result findAlbumTrackPage(@PathVariable Long albumId,
                                     @PathVariable Integer limit,
                                     @PathVariable Integer page) {
        //需要通过用户id判断是否是VIP，来衡量能不能查看声音
        Long userId = AuthContextHolder.getUserId();
        //封装page对象
        Page<AlbumTrackListVo> albumTrackListVoPage = new Page<>(page,limit);
        IPage<AlbumTrackListVo> iPage = trackInfoService.findAlbumTrackPage(albumId,albumTrackListVoPage,userId);
        return Result.ok(iPage);
    }

    @TsLogin
    @Operation(summary = "获取用户声音分集购买支付列表")
    @GetMapping("/findUserTrackPaidList/{trackId}")
    public Result<List<Map<String, Object>>> findUserTrackPaidList(@PathVariable Long trackId) {
        Long userId = AuthContextHolder.getUserId();
        // 获取购买记录集合
        List<Map<String,Object>> map = trackInfoService.findUserTrackPaidList(trackId,userId);
        return Result.ok(map);
    }
    @TsLogin
    @Operation(summary = "批量获取下单付费声音列表")
    @GetMapping("findPaidTrackInfoList/{trackId}/{trackCount}")
    public Result<List<TrackInfo>> findPaidTrackInfoList(@PathVariable Long trackId, @PathVariable Integer trackCount) {
        //	调用服务层方法
        List<TrackInfo> trackInfoList = trackInfoService.findPaidTrackInfoList(trackId, trackCount);
        //	返回数据列表
        return Result.ok(trackInfoList);
    }
}

